<template>
    <div>
        <div class="codrops-header" v-text="title"></div>
        <Row style="margin-top: 10px">
            <Button @click="add"  type="primary" icon="md-add">添加</Button>
            <Button @click="back" type="primary" icon="ios-undo">返回</Button>
        </Row>
        <div style="margin-top:10px;">
            <rui-page :defines="defines">
                <rui-table :defineId="'attachmentRuleInfo'"
                           :slots="[{key:'action',slot:'action'},{key:'send',slot:'send'}]"
                           :select="true"
                           :showIndex="false"
                           @loadDatas="getAttachmentRule"
                           ref="attachmentRuleInfoTable">
                    <template slot="action" slot-scope="{ row, index }">
                        <Button @click="edit(row)" size="small" type="primary">编辑</Button>
                        <Button @click="configure(row)" size="small" type="error">规则配置</Button>
                        <Button v-if="row.ruleId!==undefined&&row.ruleState==='1'" style="margin-right:5px" type="warning" size="small"
                                @click="invalid(row)">生效
                        </Button>
                        <Button v-if="row.ruleState==='2'" style="margin-right:5px" type="success" size="small"
                                @click="invalid(row)">失效
                        </Button>
                    </template>
                    <template slot="send" slot-scope="{ row, index }">
                        <Button v-if="row.ruleId!==undefined" style="margin-right:5px" type="success" size="small"
                                @click="synchro(row)">数据同步
                        </Button>
                    </template>
                </rui-table>
            </rui-page>
        </div>
        <Modal :title="attachmentTitle" v-model="attachmentModalVisible" :mask-closable='false' :width="400" :height="100">
            <Form ref="attachmentRuleForm" :model="attachmentRuleForm" :label-width="80" :rules="attachmentRuleFormValidate">
                <FormItem label="规则名称" prop="ruleName" style="margin-top: 20px">
                    <Input v-model="attachmentRuleForm.ruleName"/>
                </FormItem>
                <FormItem label="输出值" prop="ruleValue">
                    <Input v-model="attachmentRuleForm.ruleValue"/>
                </FormItem>
            </Form>
            <div slot="footer">
                <Button type="text" @click="cancelRule">取消</Button>
                <Button type="primary" :loading="submitLoading" @click="submit">提交</Button>
            </div>
        </Modal>
        <div>
            <Modal v-model="ruleModal" :width="1100" :styles="{top: '90'}" @on-cancel="cancel" :footer-hide="true" style="margin-left: 300px">
                <p slot="header" style="color:#f60;text-align:center">
                    <Icon type="ios-information-circle"></Icon>
                    <span>附件规则配置</span>
                </p>
                <Row>
                    <Card style="margin-top:-15px">
                        <rule-tree v-if="dataLoad" @saved="ruleSaved"
                                   :rule-data="ruleData"
                                   :ruleAtoms="ruleAtoms" :rule-id="ruleId" :ruleName="ruleName" :ruleGroup="ruleGroup" :businessScenario="businessScenario"/>
                    </Card>
                </Row>
            </Modal>
        </div>
    </div>
</template>

<script>
    let attachmentRuleInfo = [
        {
            key: 'action',
            title: '操作',
            align: 'center',
            slot: 'action'
        },
        {
            key: 'ruleName',
            title: '规则名称',
            align: 'center',
        },
        {
            key: 'ruleValue',
            title: '输出值',
            align: 'center',
        },
        {
            key: 'ruleState',
            title: '状态',
            align: 'center',
            render: (h, params) => {
                let text = '';
                let color = '';
                let arr = params.row.ruleState;
                let id = params.row.ruleId;
                if(id!==undefined&&arr==="1"){
                    text = '失效';
                    color = '';
                }else if (arr==="2"&&id!=="") {
                    text = '生效';
                    color = 'lightgreen';
                }else if(id===undefined&&arr==="1"){
                    text = '未创建';
                }
                return h('Tag', {
                        props: {
                            type:"dot",
                            color: color
                        }
                    },
                    text,
                )
            }
        },
        {
            key: 'createTime',
            title: '创建时间',
            align: 'center',
            format:'yyyy-MM-dd'
        }
    ];
    import {addAttachmentRule,queryAttachmentRule,updateAttachmentRule,ruleSynchro} from "@/projects/basic/api/image/image-manage.js";
    import ruiRules from '_c/rui-auto/rui-rules/index'
    import {loadRuleDataById} from '_p/basic/api/rule/rule'
    import {queryAtomsByBizType} from '_p/basic/api/rule/ruleAtom'
    export default {
        name: "attachmentRule",
        props: ['checkid'],
        components: {
            ruleTree:ruiRules
        },
        data(){
            return{
                submitLoading:false,
                id:"",
                title:"",
                attachmentTitle:"规则编辑",
                attachmentModalVisible:false,
                modalType: 0,
                searchForm: {
                    id:"",
                    attachmentId:"",
                    pageNumber: 1,
                    pageSize: 10,
                },
                attachmentRuleForm:{
                    id:"",
                    attachmentId:"",
                    ruleName:"",
                    ruleState:"",
                    ruleValue:"",
                },
                attachmentRuleFormValidate:{
                    ruleValue: [{required: true, message: "输出值不能为空", trigger: "change"}],
                    ruleName: [{required: true, message: "规则名称不能为空", trigger: "change"}]
                },
                //规则变量
                ruleModal:false,
                ruleAtoms:[],
                ruleName:'附件',
                ruleGroup:'',
                businessScenario:'',
                ruleData:[],
                dataLoad:false,
                bizType: 'attachment',
                ruleId:'',
                requestPath:'',
            }
        },
        methods: {
            //添加
            add(){
                this.$refs.attachmentRuleForm.resetFields();
                this.attachmentModalVisible = true;
            },
            //编辑
            edit(v){
                this.modalType = 1;
                this.$refs.attachmentRuleForm.resetFields();
                for (let attr in v) {
                    if (v[attr] === null) {
                        v[attr] = "";
                    }
                }
                let str = JSON.stringify(v);
                let member = JSON.parse(str);
                this.attachmentRuleForm = member;
                this.attachmentRuleForm.attachmentId = this.id;
                this.attachmentModalVisible = true;
            },
            //返回
            back() {
                this.$refs.attachmentRuleInfoTable.updateTableData(null, 0);
                this.$emit("back");
            },
            //取消
            cancelRule(){
                this.attachmentModalVisible = false;
            },
            //提交
            submit(){
                this.$refs.attachmentRuleForm.validate(valid => {
                    if(valid){
                        if(this.modalType === 0){
                            addAttachmentRule(this.attachmentRuleForm,this.requestPath).then(res=>{
                                if(res.code === "0000"){
                                    this.$Message.success("操作成功");
                                    this.attachmentModalVisible = false;
                                    this.getAttachmentList();
                                }
                            })
                        }else{
                            this.submitLoading = true;
                            addAttachmentRule(this.attachmentRuleForm,this.requestPath).then(res => {
                                this.submitLoading = false;
                                if (res.code === "0000") {
                                    this.$Message.success("操作成功");
                                    this.attachmentRuleForm.id ="";
                                    this.attachmentRuleForm.ruleValue ="";
                                    this.attachmentRuleForm.ruleName ="";
                                    this.attachmentRuleForm.rule = "";
                                    this.attachmentModalVisible = false;
                                    this.getAttachmentList();
                                }
                            });
                        }
                    }
                })
            },
            getAttachmentRule(pageNumber, pageSize){
                this.searchForm.pageNumber = pageNumber;
                this.searchForm.pageSize = pageSize;
                queryAttachmentRule(this.searchForm,this.requestPath).then(res=>{
                    if(res.code = "0000"){
                        let {records, total} = res.data;
                        this.$refs.attachmentRuleInfoTable.updateTableData(records, total);
                    }
                })
            },
            getAttachmentList(){
                queryAttachmentRule(this.searchForm,this.requestPath).then(res=>{
                    if(res.code = "0000"){
                        let {records, total} = res.data;
                        this.$refs.attachmentRuleInfoTable.updateTableData(records, total);
                    }
                })
            },
            //失效,失效
            invalid(row){
                updateAttachmentRule(row,this.requestPath).then(res=>{
                    if(res.code==="0000"){
                        this.$Message.success("操作成功");
                        this.getAttachmentList();
                    }
                })
            },
            //数据同步
            synchro(row){
                ruleSynchro(row,this.requestPath).then(res=>{
                    if(res.code==="0000"){
                        this.$Message.success("操作成功");
                    }
                })
            },
            ruleSaved(ruleId){
                this.ruleModal = false;
                this.cancel();
                this.getAttachmentList();
            },
            //规则配置
            configure(v){
                if(v.ruleState==="2"&&v.ruleId!==undefined){
                    this.$Message.warning("请先设置规则失效");
                    return;
                }
                this.ruleModal = true;
                if(v.ruleId!==undefined){
                    this.ruleId = v.ruleId;
                    this.businessScenario = v.id;
                }else {
                    this.ruleId = "";
                    this.businessScenario = v.id;
                }
                this.searchForm.id = v.id;
                this.queryRuleDataById();
                this.queryRuleAtomInfo();
            },
            queryRuleDataById(){
                loadRuleDataById((this.ruleId&&this.ruleId!='')?this.ruleId:undefined,this.requestPath).then(res=>{
                    if(res.code='0000'){
                        if(res.data.length>0) {
                            this.ruleData.push(...res.data);
                        }
                        this.dataLoad = true;
                    }
                });
            },
            queryRuleAtomInfo(){
                queryAtomsByBizType(this.bizType,this.requestPath).then(res=>{
                    if(res.code=="0000"){
                        this.ruleAtoms = res.data;
                    }
                })
            },
            cancel () {
                this.ruleId = '';
                this.ruleData = [];
                this.dataLoad = false;
                this.businessScenario = '';
            },
        },
        watch:{
            checkid:function (newValue) {
                if(newValue!==""){
                    this.title = newValue.attachmentName+"规则配置";
                    this.attachmentRuleForm.attachmentId = newValue.id;
                    this.searchForm.attachmentId = newValue.id;
                    this.id = newValue.id;
                    this.ruleGroup = newValue.busiNode;
                    if(this.$route.meta.params&&this.$route.meta.params.requestPath&&this.$route.meta.params.requestPath!==''){
                        this.requestPath = this.$route.meta.params.requestPath;
                    }
                    this.getAttachmentRule(1,10);
                }
            }
        },
        computed: {
            defines() {
                return [
                    {
                        id: 'attachmentRuleInfo',
                        fields: attachmentRuleInfo
                    }
                ]
            }
        },
    }
</script>
<style>
    .codrops-header {
        display: block;
        font-size: 250%;
        font-weight: 400;
        text-align: center;
        padding-top: 0.325em;
        color: #FA8072;
    }
    .amt_list_type .ivu-form-item-label{
        text-align:left;
    }
</style>
